import socket

class GB28181Config:
    _instance = None
    
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
            cls._instance._init_config()
            cls._instance._validate_config()
        return cls._instance
    
    def _init_config(self):
        # ===== WVP适配配置 =====
        # 设备配置 - 符合WVP要求
        self.device_id = "41010500002000000001"  # 20位国标编号，WVP提供
        self.username = self.device_id
        self.password = "12345678"  # WVP密码
        self.domain = "4101050000"  # WVP域
        self.channel_id = "41010500001320000001"  # 通道编号，132为摄像机通道类型
        
        # SIP服务器配置 - WVP云端服务器
        self.sip_server_ip = "101.126.17.13"  # WVP云端服务器IP地址
        self.sip_server_port = 8116  # WVP SIP端口
        self.sip_server_id = "41010500002000000001"  # WVP服务器ID
        
        # 本地配置 - 数据模拟电脑
        self.local_ip = "192.168.31.104"  # 本地数据模拟电脑IP
        self.local_port = 5060  # 客户端SIP端口
        
        # TCP被动模式配置 - 匹配ZLM端口范围
        self.tcp_passive = False  # 禁用TCP被动模式，改用UDP主动模式
        self.tcp_media_port = 30000  # TCP媒体端口，使用ZLM端口范围30000-30200
        self.local_media_port = 9000  # 本地媒体端口，用于SDP响应
        
        # 设备信息 
        self.device_info = {
            "name": "GB28181 Camera Device",  # 设备名称
            "manufacturer": "MyCompany",   # 制造商
            "model": "IPCamera Pro",      # 型号
            "firmware": "v2.0.1",         # 固件版本
            "channels": [
                {
                    "id": self.channel_id,
                    "name": "Main Camera Channel",
                    "status": "ON",
                    "ptz_type": 1,  # PTZ类型
                    "resolution": "640*480"  # 分辨率
                }
            ]
        }
        
        # 心跳和注册配置
        self.keepalive_interval = 30  # 30秒心跳间隔
        self.register_expires = 3600  # 注册过期时间
        
        # 媒体相关
        self.video_file = "src/recordfile01.h264"  # 暂时使用H.264文件，避免ffmpeg依赖
    
    def _validate_config(self):
        """验证配置"""
        print(f"本地IP: {self.local_ip}")
        print(f"设备ID: {self.device_id}")
        print(f"通道ID: {self.channel_id}")
        print(f"SIP服务器: {self.sip_server_ip}:{self.sip_server_port}")